﻿// -----------------------------------------------------------------------
// <copyright file="PhieuPhauThuatThuThuatDiChuyenChiTietDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class PhieuPhauThuatThuThuatDiChuyenChiTietDataAccess
    {
        public bool InsertPhieuPhauThuatThuThuatDiChuyenChiTiet(PhieuPhauThuatThuThuatDiChuyenChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhauThuatThuThuat", DbType.String, obj.MaPhauThuatThuThuat);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertPhieuPhauThuatThuThuatDiChuyenChiTiet", ex.Message); throw ex; }
        }

        public bool EditPhieuPhauThuatThuThuatDiChuyenChiTiet(PhieuPhauThuatThuThuatDiChuyenChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhauThuatThuThuat", DbType.String, obj.MaPhauThuatThuThuat);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdatePhieuPhauThuatThuThuatDiChuyenChiTiet", ex.Message); throw ex; }
        }

        public bool DeletePhieuPhauThuatThuThuatDiChuyenChiTiet(string maPhauThuatThuThuat, string maChiTieu)
        {
            try
            {
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhauThuatThuThuat", DbType.String, maPhauThuatThuThuat);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuPhauThuatThuThuatDiChuyenChiTiet", ex.Message); throw ex; }
        }

        public bool DeletePhieuPhauThuatThuThuatDiChuyenChiTiet_ByMaPhauThuatThuThuat(string maPhauThuatThuThuat)
        {
            try
            {
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_DeleteByMaPhieuPhauThuatThuThuat";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhauThuatThuThuat", DbType.String, maPhauThuatThuThuat);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuPhauThuatThuThuatDiChuyenChiTiet_ByMaPhauThuatThuThuat", ex.Message); throw ex; }
        }

        public PhieuPhauThuatThuThuatDiChuyenChiTietEntity GetPhieuPhauThuatThuThuatDiChuyenChiTiet(string maPhauThuatThuThuat, string maChiTieu)
        {
            try
            {
                PhieuPhauThuatThuThuatDiChuyenChiTietEntity obj = null;
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhauThuatThuThuat", DbType.String, maPhauThuatThuThuat);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuPhauThuatThuThuatDiChuyenChiTietEntity();
                        obj.MaPhauThuatThuThuat = reader["MaPhauThuatThuThuat"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuPhauThuatThuThuatDiChuyenChiTiet", ex.Message); throw ex; }
        }

        public List<PhieuPhauThuatThuThuatDiChuyenChiTietEntity> GetPhieuPhauThuatThuThuatDiChuyenChiTietList()
        {
            try
            {
                List<PhieuPhauThuatThuThuatDiChuyenChiTietEntity> list = new List<PhieuPhauThuatThuThuatDiChuyenChiTietEntity>();
                PhieuPhauThuatThuThuatDiChuyenChiTietEntity obj = null;
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuPhauThuatThuThuatDiChuyenChiTietEntity();
                        obj.MaPhauThuatThuThuat = reader["MaPhauThuatThuThuat"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuPhauThuatThuThuatDiChuyenChiTietList", ex.Message); throw ex; }
        }

        public List<PhieuPhauThuatThuThuatDiChuyenChiTietEntity> GetPhieuPhauThuatThuThuatDiChuyenChiTietByMaPhieuList(string maPhieuPhauThuat)
        {
            try
            {
                List<PhieuPhauThuatThuThuatDiChuyenChiTietEntity> list = new List<PhieuPhauThuatThuThuatDiChuyenChiTietEntity>();
                PhieuPhauThuatThuThuatDiChuyenChiTietEntity obj = null;
                string sql = "spPhieuPhauThuatThuThuatDiChuyenChiTiet_SelectByMaPhieuThuThuat";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaPhauThuatThuThuat", DbType.String, maPhieuPhauThuat);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuPhauThuatThuThuatDiChuyenChiTietEntity();
                        obj.MaPhauThuatThuThuat = reader["MaPhauThuatThuThuat"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuPhauThuatThuThuatDiChuyenChiTietByMaPhieuList", ex.Message); throw ex; }
        }

    }
}